package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XuechengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Resource
    private CourseTeacherMapper courseTeacherMapper;
    @Resource
    private CourseBaseMapper courseBaseMapper;
    @Override
    public List<CourseTeacher> getCourseTeacher(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);
        return courseTeachers;
    }

    @Override
    public CourseTeacher addCourseTeacher(CourseTeacher courseTeacher) {
        //TODO 公司id暂时为固定值
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if(!courseBase.getCompanyId().equals(1232141425L))
            throw new XuechengPlusException("只允许向机构自己的课程中删除老师。");
        courseTeacher.setCreateDate(LocalDateTime.now());
        int insert = courseTeacherMapper.insert(courseTeacher);
        if(insert<=0){
            throw new XuechengPlusException("添加教师失败");
        }
        return courseTeacher;
    }

    @Override
    public CourseTeacher updateCourseTeacher(CourseTeacher courseTeacher) {
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if(!courseBase.getCompanyId().equals(1232141425L))
            throw new XuechengPlusException("只允许向机构自己的课程中修改老师。");
        int i = courseTeacherMapper.updateById(courseTeacher);
        if(i<=0){
            throw new XuechengPlusException("修改教师信息失败");
        }
        return courseTeacherMapper.selectById(courseTeacher);
    }

    @Override
    public void deleteCourseTeacher(Long courseId, Long id) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(!courseBase.getCompanyId().equals(1232141425L))
            throw new XuechengPlusException("只允许向机构自己的课程中删除老师。");
        LambdaQueryWrapper<CourseTeacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        queryWrapper.eq(CourseTeacher::getId,id);
        int delete = courseTeacherMapper.delete(queryWrapper);
        if(delete<=0){
            throw new XuechengPlusException("删除教师失败");
        }
    }
}
